﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using ByteSite.Common;

namespace ByteSite.BLL
{
    /// <summary>
    ///请求回复规格
    /// </summary>
    public partial class weixin_request_rule
    {
        private readonly Entitys.sysconfig sysConfig = new BLL.sysconfig().loadConfig();//获得系统配置信息
        private readonly DAL.Data data;

        public weixin_request_rule()
        {
            data = new DAL.Data();
        }

        #region 基本方法================================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return data.Exists<Entitys.dt_weixin_request_rule>(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Entitys.dt_weixin_request_rule model)
        {
            return data.Insert<Entitys.dt_weixin_request_rule>(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Entitys.dt_weixin_request_rule model)
        {
            return data.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            return data.Delete<Entitys.dt_weixin_request_rule>(id);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Entitys.dt_weixin_request_rule Get(int id)
        {
            return data.Get<Entitys.dt_weixin_request_rule>(id);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public List<Entitys.dt_weixin_request_rule> GetList(int Top, string strWhere, string filedOrder)
        {
            return data.List<Entitys.dt_weixin_request_rule>(Top, strWhere, filedOrder);
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public List<Entitys.dt_weixin_request_rule> GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return data.List<Entitys.dt_weixin_request_rule>(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }
        #endregion

        #region 扩展方法================================
        /// <summary>
        /// 修改一列数据
        /// </summary>
        public void UpdateField(Expression<Func<Entitys.dt_weixin_request_rule, Entitys.dt_weixin_request_rule>> expression, Expression<Func<Entitys.dt_weixin_request_rule, bool>> whereExpression)
        {
            data.UpdateColumns<Entitys.dt_weixin_request_rule>(expression, whereExpression);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Entitys.dt_weixin_request_rule Get(int account_id, int request_type)
        {
            return data.Get<Entitys.dt_weixin_request_rule>(x => x.account_id == account_id && x.request_type == request_type);
        }
        #endregion

        #region 微信通讯方法===============================
        /// <summary>
        /// 得到规则ID以回复类型
        /// </summary>
        public int GetRuleIdAndResponseType(int account_id, int request_type, out int response_type)
        {
            Entitys.dt_weixin_request_rule model = data.Get<Entitys.dt_weixin_request_rule>(x => x.account_id == account_id && x.request_type == request_type);
            response_type = Utils.ObjToInt(model.request_type);
            return model.id;
        }

        /// <summary>
        /// 得到关健字查询的规则ID及回复类型
        /// </summary>
        public int GetKeywordsRuleId(int account_id, string keywords, out int response_type)
        {
            int rule_id = 0;
            //精确匹配
            Entitys.dt_weixin_request_rule model3 = data.Get<Entitys.dt_weixin_request_rule>(x => x.account_id == account_id && x.request_type == 1 && (x.keywords.Contains(keywords + "|") || x.keywords.Contains("|" + keywords + "|") || x.keywords.Contains("|" + keywords) || x.keywords.Contains(keywords)), x => x.sort_id, SqlSugar.OrderByType.Asc);
            if (model3 != null)
            {
                rule_id = model3.id;
                response_type = Utils.ObjToInt(model3.response_type);
                return rule_id;
            }
            //strSql3.Append("select top 1 id,response_type from " + databaseprefix + "weixin_request_rule");
            //strSql3.Append(" where account_id=" + account_id + " and request_type=1");
            //strSql3.Append(" and(keywords like '" + keywords + "|%' or keywords='%|" + keywords + "' or keywords like '%|" + keywords + "|%' or keywords='" + keywords + "')");
            //strSql3.Append(" order by sort_id asc,add_time desc");

            //模糊匹配
            Entitys.dt_weixin_request_rule model2 = data.Get<Entitys.dt_weixin_request_rule>(x => x.account_id == account_id && x.request_type == 1 && x.keywords.Contains(keywords), x => x.sort_id, SqlSugar.OrderByType.Asc);
            if (model2 != null)
            {
                rule_id = model2.id;
                response_type = Utils.ObjToInt(model2.response_type);
                return rule_id;
            }
            //strSql2.Append("select top 1 id,response_type from " + databaseprefix + "weixin_request_rule");
            //strSql2.Append(" where account_id=" + account_id + " and request_type=1 and keywords like '%" + keywords + "%'");
            //strSql2.Append(" order by sort_id asc,add_time desc");

            //默认回复

            Entitys.dt_weixin_request_rule model1 = data.Get<Entitys.dt_weixin_request_rule>(x => x.account_id == account_id && x.request_type == 0, x => x.sort_id, SqlSugar.OrderByType.Asc);
            if (model1 != null)
            {
                rule_id = model1.id;
                response_type = Utils.ObjToInt(model1.response_type);
                return rule_id;
            }
            response_type = 0;
            return rule_id;
        }
        #endregion
    }
}